$(document).ready(function() {
	proInfoQuery.init();
    proInfoQuery.getsourcelist();
});
var _mmg = null;
var proInfoQuery = function(){
    return{
        init: function () {
            _mmg = $('#dg').datagrid({
                url: '/proInfoPage',
                params: $("#proInfoForm").serialize(),
                method: 'post',
                rownumbers: true,
                fitColumns: true,
                striped: true,
                nowrap: true,
                showFooter: true,
                pagination: true,
                pageList: [25, 30, 35],
                idField: 'id', //主键
                columns: [
                    [
                        {field: 'ck', checkbox: true},
                        {field: 'id', title: 'id', hidden: true},
                        {field: 'name', title: '商品名称', width: 160},
                        {field: 'sku', title: 'SKU', width: 40},
                        {field: 'price', title: '价格/元', width: 40},
                        {field: 'comment', title: '评价数量', width: 40},
                        {field: 'sales', title: '月销量/笔', width: 40},
                        {field: 'source', title: '信息来源', width: 40},
                        {field: 'creattime', title: '创建时间', width: 45},
                        {field: 'link', title: '商品链接', width: 140},
                        // {field:'imgurl', title:'图片地址', width:120},
                    ]
                ],

            }).datagrid("clientPaging");
        },
        
           //初始化网站下拉框
        getsourcelist: function () {
            $('#source_info').combobox(
                {
                    url: '/getsourcelist',
                    valueField: 'id',
                    textField: 'text',
                    method: 'POST'
                }
            )
            //      $.ajax({
            //         type: "post",
            //         url: '/getcountrylist',
            //         success: function (result) {
            //            
            //              var str={"firstName":"Brett"}
            //              $("#source_info").combobox('setValue', str['firstName']);
            //         }
            //     });
        },
        //查询
        load: function () {
            var pro_name = $("#pro_name").val();
            var first_price = $("#first_price").val();
            var last_price = $("#last_price").val();
            var source_info = $("#source_info").combobox('getValue');
            var firsttime = $("#firsttime").datetimebox('getValue');
            var lasttime = $("#lasttime").datetimebox('getValue');
           
            $('#dg').datagrid('load', {
                pro_name: pro_name,
                first_price: first_price,
                last_price: last_price,
                source_info: source_info,
                firsttime: firsttime,
                lasttime: lasttime
            });
        },

         //打开弹出窗
        addPopu: function () {
           $('#w').window('open');
        },
         //关闭_弹出框
        closePopu: function () {
            $('#w').window('close', true);

        },

         //删除功能
       del: function () {
            var chackedItem = $('#dg').datagrid('getChecked');
            var integral_ids = [];
            var ids;
            if (chackedItem.length == '0') {
                alert("请至少选择一条记录！");
                return;
            }
            $.messager.confirm('确认', '您确定要删除当前选中的记录吗？', function (r) {
                if (r) {
                    if (chackedItem.length == 1) {
                        //删除单条记录
                        var chackedItem1 = $('#dg').datagrid('getChecked');
                        var integral_id;
                        $.each(chackedItem1, function (index, item) {
                            integral_id = item.id;
                        });
                        $.ajax({
                            type: 'POST',
                            url: '/del_Integral_by_id',
                            data: 'id=' + integral_id,
                            success: function (result) {
                                if (result == '1') {
                                    alert("删除成功")
                                    proInfoQuery.load();
                                    return;
                                } else {
                                    alert('删除失败！');
                                    return;
                                }
                            }
                        })
                    } else {
                        //删除多条记录
                        $.each(chackedItem, function (index, item) {
                            if (integral_ids == "") {
                                integral_ids = "" + item.id + "";
                                
                            } else {
                                integral_ids = "" + item.id + "" + "," + integral_ids;
                                ids = integral_ids.split(',');
                            }
                        });
                        $.ajax({
                            type: 'POST',
                            url: '/del_Integral_by_ids',
                            data: 'id=' + JSON.stringify(ids),
                            success: function (result) {
                                if (result == '1') {
                                    alert('删除成功！');
                                    proInfoQuery.load();
                                    return;
                                } else {
                                    alert('删除失败！');
                                    return;
                                }
                            }
                        })
                    }
                }
            });
        },
         //查看价格走势
        see: function () {
            var link
            var source
            var chackedItem = $('#dg').datagrid('getChecked');
            var sku;
            if (chackedItem.length == '0' || chackedItem.length > '1') {
                alert("请选择一条记录！");
                return;
            }
            $.messager.confirm('确认', '您确定要查看该商品的价格走势？', function (r) {
                if (r) {
                    $.each(chackedItem, function (index, item) {
                        source = item.source;
                        sku = item.sku;
                        if (source == '京东') {
                            link="http://www.gwdang.com/trend/"+sku+"-3.html?static=true"
                            // link = 'http://www.xitie.com/360buy.php?no=' + sku
                           console.info(item.link)
                            // window.location.href = link;
                             window.open(link)
                        } else if (source == '天猫') {
                            
                            // link = 'http://www.xitie.com/tmall.php?no=' + sku
                            link="http://www.gwdang.com/crc64/trend/"+sku+".html?static=true"
                            // window.location.href = link;
                            window.open(link)
                        }else if (source == '当当') {
                            // link = 'http://www.xitie.com/tmall.php?no=' + sku
                            link="http://www.gwdang.com/crc64/trend/"+sku+"-2.html?static=true"
                            // window.location.href = link;
                            window.open(link)
                        }
                    });
                }
            });


        },
       //清空表单
        clearForm: function (formID, unload) {
            $("#" + formID).each(function () {
                $("#jd_name").val('')
                $("#first_price").val('')
                $("#last_price").val('')
                $("#firsttime").datetimebox('clear');
                $("#lasttime").datetimebox('clear');
                $('#source_info').combobox('clear');
                // iterate the elements within the form
                $(':input', this).each(function () {
                    var type = this.type, tag = this.tagName.toLowerCase();
                    if (type == 'text' || type == 'password' || tag == 'textarea' || type == "file")
                        this.value = '';
                    else if (type == 'checkbox' || type == 'radio')
                        this.checked = false;
                    else if (tag == 'select')
                        $('#source_info').combobox('clear');
                });
                $('#dg').datagrid('load', {});
            });
        }

    }

}
();

